<?php
/**
 * 名人机构
 * 
 * @author 	jiaowei<jiaowei@178.com>
 * @version $Id: star.php 6709 2010-05-20 10:04:21Z jiaowei $
 */
class friends_controller_star extends Controller
{	
	/**
	 * friends_model_star
	 *
	 * @var obj
	 */
	private $_star = null;
	
	/**
	 * 模块缓存前缀
	 *
	 * @var str
	 */
	private $_module = null;
	
	public function __construct($app, $controller)
	{
		parent::__construct($app, $controller);
		$this->headerView = $this->viewDir . '/_header.phtml';
		$this->_star = new friends_model_star();
		$this->_module = $app . '_' . $controller;
		$this->requireLogin();
	}
	
	/**
	 * 名人机构列表
	 *
	 */
	public function index_action()
	{
		$cid_1  = (int) _('cid_1', 8);//一级分类Id 默认为8
		$cid_2	= (int) _('cid_2', 0);//二级分类Id 默认为0
		
		$uid  = (int) self::$login['uid'];
		
		//获取当前分类下的名人机构列表id
		$stars = $this->_star->getStars($cid_1, $cid_2);
		
		//获取名人结构详细信息
		$cacheId = array();
		foreach ($stars as $k => $v) {
			$cacheId[] = $v['id'];
		}
		$usersInfo = friends_util_friendutil::getUserInfo($cacheId, $uid);
		
		//获取机构下子机构详细信息
		foreach ($usersInfo as $k => $v) {
			$usersInfo[$k]['child_users'] = array();
			if ($v['childs']) {
				$childIds = unserialize($v['childs']);
				$usersInfo[$k]['child_users'] = friends_util_friendutil::getUserInfo($childIds, $uid);
			}
		}
		print_r($usersInfo);
	}
	
	/**
	 * 我加的名人机构
	 *
	 */
	public function my_action()
	{
		$uid  = (int) self::$login['uid'];
		
		$stars = $this->_star->myStars($uid);
		$cacheId = array();
		foreach ($stars as $k => $v) {
			$cacheId[] = $v['id'];
		}
		$starsInfo = friends_util_friendutil::getUserInfo($cacheId, $uid);
		
	}
	
	/**
	 * 好有加的名人机构
	 *
	 */
	public function friends_action()
	{
		$uid  = (int) self::$login['uid'];
		$friends = friends_util_friendutil::getFriendIds($uid);
		
		$stars = $this->_star->friendsStars($friends);
		$cacheId = array();
		foreach ($stars as $k => $v) {
			$cacheId[] = $v['id'];
		}
		$starsInfo = friends_util_friendutil::getUserInfo($cacheId, $uid);
	}
	
	/**
	 * 添加名人机构
	 * 
	 * 名人添加为好友，机构添加一条记录为单项好友
	 *
	 */
	public function addstar_action()
	{
		$this->_renderJson();
		$uid  	= (int) self::$login['uid'];//当前登陆ID
		$starId = (int) _('id', 0);//名人机构ID
		if (!$starId) {
			$this->ret = self::failure(Error::ERROR_WRONG_PARAMS, '添加错误');return;
		}
		
		//获取用户名和用户类别
		$userInfo = $this->_star->getUserInfo($uid);
		if (!$userInfo) {
			$this->ret = self::failure(Error::ERROR_WRONG_PARAMS, '添加错误');return;
		}
		$userType = (int) $userInfo['type'];
		
		if (1 == $userType) {//名人加为好友
			$rel_1 = array(
				'uid' 		=> $uid,
				'username'  => self::$login['nickname'],
				'host_id'   => $starId,
				'is_friend' => 1
			);
			$rel_2 = array(
				'uid' 		=> $starId,
				'username'  => $userInfo['username'],
				'host_id'   => $uid,
				'is_friend' => 1
			);
			//开始事务
			$this->_mod->db->query("BEGIN");
			
			$flag1 = Model::factory('friends_model_data')->save($rel_1);
			$flag2 = Model::factory('friends_model_data')->save($rel_2);
			
			if ($flag1 && $flag2) {
				$this->_mod->db->query("COMMIT");
			} else {
				$this->_mod->db->query("ROLLBACK");
				$this->ret = self::failure(Error::ERROR_WRONG_PARAMS, '添加错误');return;
			}			
		} elseif (2 == $userType) {//机构成为粉丝
			$rel = array(
				'uid' 		=> $uid,
				'username'  => self::$login['nickname'],
				'host_id'   => $starId,
				'is_friend' => 2
			);
			if (!Model::factory('friends_model_data')->save($rel)) {
				$this->ret = self::failure(Error::ERROR_WRONG_PARAMS, '添加错误');return;
			}
		} else {}
		
		$this->ret = self::success();
	}
	
	/**
	 * 随机显示一个名人或机构
	 *
	 */
	public function rand_action()
	{
		
	}
	
	/**
	 * 获取一级分类
	 *
	 */
	public function parentCate()
	{
		$cates = $this->_star->getParentCate();
	}
	
	/**
	 * 获取二级分类
	 *
	 */
	public function childCate()
	{
		$cid = (int) _('id', 0);//分类ID
		$cates = $this->_star->getChildCate($cid);
	}
	
	
	
	
	

}
